package com.eastjet.maintenance.contrller;

import com.alibaba.nacos.common.JustForTest;
import com.eastjet.maintenance.config.Code;
import com.eastjet.maintenance.config.Result;
import com.eastjet.maintenance.domain.DeviceType;
import com.eastjet.maintenance.domain.Equipment;
import com.eastjet.maintenance.domain.StorageLocation;
import com.eastjet.maintenance.domain.TypePart;
import com.eastjet.maintenance.feign.DictDataFeign;
import com.eastjet.maintenance.service.impl.DeviceTypeServiceImpl;
import com.eastjet.maintenance.service.impl.StorageLocationServiceImpl;
import com.eastjet.maintenance.service.impl.TypePartServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysDictData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 设备类型/存放位置
 */
@RestController
@CrossOrigin(origins = "*")
public class TypeController {

    @Autowired
    private DeviceTypeServiceImpl deviceTypeService;// 设备类型serviceimp
    @Autowired
    private StorageLocationServiceImpl storageLocationService;// 存储位置serviceimp
    @Autowired
    private TypePartServiceImpl typePartService;// 备件类型serviceimp

    @Autowired
    private DictDataFeign dictDataFeign;//远程调用字典服务

    /**
     * 新增设备类型
     *
     * @param typer
     * @return
     */
    @PostMapping("/InsertSbType")
    public Result InsertDeviceType(@RequestBody DeviceType typer) {
        Boolean b = deviceTypeService.InsertType(typer);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增成功" : "新增失败");
    }

    /**
     * 查询设备类型
     *
     * @return
     */
    //查询设备类型
    @RequestMapping("select")
    public Result SelecAll() {
        List<DeviceType> deviceTypeList = deviceTypeService.findDeviceTypeList();// 查询所有父级设备类型
        List<DeviceType> deviceTypeList2 = deviceTypeService.findTypeList();// 查询所有子集级设备类型
        List<DeviceType> deviceTypeList3 = new ArrayList<>();
        for (DeviceType ty : deviceTypeList) {
            int index = ty.getName().indexOf(">");
            if (index != -1) {

            } else {
                ty.setName(">" + ty.getName());
            }
            deviceTypeList3.add(ty);
            for (DeviceType t2 : deviceTypeList2) {
                if (ty.getId().equals(t2.getFu())) {
                    deviceTypeList3.add(t2);
                }
            }
        }
        return new Result(deviceTypeList3, deviceTypeList.isEmpty() ? Code.GET_ERR : Code.GET_OK, deviceTypeList.isEmpty() ? "查询失败" : "查询成功");
    }

    /*
     * 搜索查询设备类型
     */
    @RequestMapping("selectTypes")
    public Result SelectDeviceType(@RequestParam("name") String name) {
        List<DeviceType> deviceTypeListByStatus = deviceTypeService.findDeviceTypeListByStatus(name);
        return new Result(deviceTypeListByStatus, deviceTypeListByStatus.isEmpty() ? Code.GET_ERR : Code.GET_OK, deviceTypeListByStatus.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 修改设备类型状态
     *
     * @param status
     * @param code
     * @return
     */
    @RequestMapping("updatetypeqi")
    public Result UpdateTypeQi(@RequestBody List<DeviceType> typer) {
        Boolean b = deviceTypeService.updateType(typer);
        return new Result(b, b ? Code.UPDATE_OK : Code.DELETE_ERR, b ? "修改成功" : "修改失败");
    }

    @PostMapping("/UpdateType")
    public Result UpdateType(@RequestBody List<DeviceType> typer) {
        Boolean b = deviceTypeService.updateType(typer);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "修改成功" : "修改失败");
    }

    /**
     * 删除设备类型
     *
     * @param code
     * @return
     */
    @RequestMapping("deleteType")
    public Result DeleteType(String code) {
        Boolean b = deviceTypeService.DeleteType(code);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "删除成功" : "删除失败");
    }

    /**
     * 新增存储位置
     *
     * @return
     */
    @PostMapping("/InsertLocation")
    public Result InsertLocation(@RequestBody StorageLocation location) {
        location.setTombstone(0);
        System.out.println("新增存储位置成功" + location);
        Boolean b = storageLocationService.InsetStorageLocation(location);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增成功" : "新增失败");
    }

    /**
     * 查询存储位置
     *
     * @return
     */
    @RequestMapping("selectLocation")
    public Result SelectLocation(@RequestParam(value = "name") String name, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StorageLocation> storageLocations = storageLocationService.SelectLocation(name);
        PageInfo<StorageLocation> pageInfo = new PageInfo<>(storageLocations);
        return new Result(pageInfo, storageLocations.isEmpty() ? Code.GET_ERR : Code.GET_OK, storageLocations.isEmpty() ? "查询失败" : "查询成功");
    }

    /**
     * 修改存储位置
     *
     * @param location
     * @return
     */
    @RequestMapping("UpdateLocation")
    public Result UpdateLocation(@RequestBody List<StorageLocation> location) {
        boolean b = storageLocationService.UpdateLocation(location);
        System.out.println("修改存储位置成功" + b);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "修改成功" : "修改失败");
    }

    /**
     * 修改存储位置状态
     *
     * @param status
     * @param code
     * @return
     */
    @RequestMapping("UpdateLocationQi")
    public Result UpdateLocationQi(String status, String code) {
        Boolean b = storageLocationService.UpdateLocationQi(code, status);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "修改成功" : "修改失败");
    }

    /**
     * 删除存储位置
     *
     * @param code
     * @return
     */
    @RequestMapping("DeleteLocation")
    public Result DeleteLocationQi(String code) {
        Boolean b = storageLocationService.DeleteLocation(code);
        return new Result(b, b ? Code.DELETE_OK : Code.DELETE_ERR, b ? "删除成功" : "删除失败");
    }

    /**
     * 查询字典数据
     *
     * @param code
     * @return
     */
    @RequestMapping("/zidian")
    public Result SelectZi(String code) {
        List<SysDictData> dictData = dictDataFeign.getDictData(code);
        return new Result(dictData, dictData.isEmpty() ? Code.GET_ERR : Code.GET_OK, dictData.isEmpty() ? "成功" : "失败");
    }

    /**
     * 查询备件类型
     *
     * @return
     */
    //查询设备类型
    @RequestMapping("selectTypePat")
    public Result SelectTypePat(TypePart typePart) {
        List<TypePart> deviceTypeList = typePartService.SelectAllTypePart(typePart);// 查询所有父级设备类型
        List<TypePart> deviceTypeList2 = typePartService.SelectAllTypePartByParentId(typePart);// 查询所有子集级设备类型
        List<TypePart> deviceTypeList3 = new ArrayList<>();
        for (TypePart ty : deviceTypeList) {
            int index = ty.getTypeName().indexOf(">");
            if (index != -1) {

            } else {
                ty.setTypeName(">" + ty.getTypeName());
            }
            deviceTypeList3.add(ty);
            for (TypePart t2 : deviceTypeList2) {
                if (ty.getId().equals(t2.getParentType())) {
                    deviceTypeList3.add(t2);
                }
            }
        }
        return new Result(deviceTypeList3, deviceTypeList.isEmpty() ? Code.GET_ERR : Code.GET_OK, deviceTypeList.isEmpty() ? "查询失败" : "查询成功");
    }

    /*
     * 新增备件类型
     */
    @PostMapping("/InsertTypePart")
    public Result InsertTypePart(@RequestBody TypePart typePart) {
        typePart.setTombstone(0);
        Boolean b = typePartService.InsertTypePart(typePart);
        return new Result(b, b ? Code.SAVE_OK : Code.SAVE_ERR, b ? "新增成功" : "新增失败");
    }

    /*
     * 修改备件类型/删除
     */
    @PostMapping("/UpdateTypePart")
    public Result UpdateTypePart(@RequestBody List<TypePart> typePart) {
        Boolean b = typePartService.UpdateTypePart(typePart);
        return new Result(b, b ? Code.UPDATE_OK : Code.UPDATE_ERR, b ? "修改成功" : "修改失败");
    }
}
